package com.jeesite.modules.demo.service;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.demo.entity.DemoCustomer;
import com.jeesite.modules.demo.dao.DemoCustomerDao;
import com.jeesite.modules.demo.entity.DemoCustomerInfo;
import com.jeesite.modules.demo.dao.DemoCustomerInfoDao;

/**
 * demo_customerService
 * @author ccz
 * @version 2025-03-19
 */
@Service
public class DemoCustomerService extends CrudService<DemoCustomerDao, DemoCustomer> {
	
	@Autowired
	private DemoCustomerInfoDao demoCustomerInfoDao;
	
	/**
	 * 获取单条数据
	 * @param demoCustomer
	 * @return
	 */
	@Override
	public DemoCustomer get(DemoCustomer demoCustomer) {
		DemoCustomer entity = super.get(demoCustomer);
		if (entity != null){
			DemoCustomerInfo demoCustomerInfo = new DemoCustomerInfo(entity);
			demoCustomerInfo.setStatus(DemoCustomerInfo.STATUS_NORMAL);
			entity.setDemoCustomerInfoList(demoCustomerInfoDao.findList(demoCustomerInfo));
		}
		return entity;
	}
	
	/**
	 * 查询分页数据
	 * @param demoCustomer 查询条件
	 * @param demoCustomer.page 分页对象
	 * @return
	 */
	@Override
	public Page<DemoCustomer> findPage(DemoCustomer demoCustomer) {
		return super.findPage(demoCustomer);
	}
	
	/**
	 * 查询列表数据
	 * @param demoCustomer
	 * @return
	 */
	@Override
	public List<DemoCustomer> findList(DemoCustomer demoCustomer) {
		return super.findList(demoCustomer);
	}
	
	/**
	 * 查询子表分页数据
	 * @param demoCustomerInfo
	 * @param demoCustomerInfo.page 分页对象
	 * @return
	 */
	public Page<DemoCustomerInfo> findSubPage(DemoCustomerInfo demoCustomerInfo) {
		Page<DemoCustomerInfo> page = demoCustomerInfo.getPage();
		page.setList(demoCustomerInfoDao.findList(demoCustomerInfo));
		return page;
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param demoCustomer
	 */
	@Override
	@Transactional
	public void save(DemoCustomer demoCustomer) {
		super.save(demoCustomer);
		// 保存 DemoCustomer子表
		for (DemoCustomerInfo demoCustomerInfo : demoCustomer.getDemoCustomerInfoList()){
			if (!DemoCustomerInfo.STATUS_DELETE.equals(demoCustomerInfo.getStatus())){
				demoCustomerInfo.setDemoCustomerId(demoCustomer);
				if (demoCustomerInfo.getIsNewRecord()){
					demoCustomerInfoDao.insert(demoCustomerInfo);
				}else{
					demoCustomerInfoDao.update(demoCustomerInfo);
				}
			}else{
				demoCustomerInfoDao.delete(demoCustomerInfo);
			}
		}
	}
	
	/**
	 * 更新状态
	 * @param demoCustomer
	 */
	@Override
	@Transactional
	public void updateStatus(DemoCustomer demoCustomer) {
		super.updateStatus(demoCustomer);
	}
	
	/**
	 * 删除数据
	 * @param demoCustomer
	 */
	@Override
	@Transactional
	public void delete(DemoCustomer demoCustomer) {
		super.delete(demoCustomer);
		DemoCustomerInfo demoCustomerInfo = new DemoCustomerInfo();
		demoCustomerInfo.setDemoCustomerId(demoCustomer);
		demoCustomerInfoDao.deleteByEntity(demoCustomerInfo);
	}
	
}